﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.IO;
using System.Windows.Media.Imaging;
using PotatoSoft.Middleware;
using PotatoSoft.VOCTS.Workbench;
using PotatoSoft.ViewModel.Messages;
using GalaSoft.MvvmLight.Messaging;

namespace PotatoSoft.ViewModel
{
    public class TreeViewItemWithIcon : TreeViewItem
    {
        public ImageSource iconSource { get; set; }
        public Image icon { get; set; }
        public TextBlock textBlock { get; set; }
        public int ItemId { get; set; }
        public TreeViewItemWithIcon Parent { get; set; }

        public static BitmapImage ProjectIconSource;
        public static BitmapImage ExaminationIconSource;
        public static BitmapImage ImageIconSource;

        static TreeViewItemWithIcon()
        {
            string dict = Directory.GetCurrentDirectory();
            ProjectIconSource = new BitmapImage(new Uri(dict + "//icon//project.png"));
            ExaminationIconSource = new BitmapImage(new Uri(dict + "//icon//exam.png"));
            ImageIconSource = new BitmapImage(new Uri(dict + "//icon//image.png"));
        }

        public TreeViewItemWithIcon()
        {
            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;
            Header = stack;
            icon = new Image();
            icon.VerticalAlignment = VerticalAlignment.Center;
            icon.Width = 20;
            icon.Margin = new Thickness(0, 0, 4, 0);
            icon.Source = iconSource;
            stack.Children.Add(icon);
            textBlock = new TextBlock();
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            stack.Children.Add(textBlock);
        }
    }
    
    public class ProjectExplorerViewModel : ViewModelBase
    {
        private VisualOctStudioCore voctsCore=VisualOctStudioCore.GetInstance();

        private ContextMenu projectContextMenu;
        private ContextMenu examContextMenu;
        private ContextMenu imageContextMenu;

        
        public ProjectExplorerViewModel()
        {
            projectContextMenu = new ContextMenu();
            MenuItem ma = new MenuItem { Header = "Add Exam" };
            MenuItem ma1 = new MenuItem { Header = "Delete Project" };
            projectContextMenu.Items.Add(ma);
            projectContextMenu.Items.Add(ma1);

            examContextMenu = new ContextMenu();
            MenuItem ma11 = new MenuItem { Header = "Add Image" };
            MenuItem ma12 = new MenuItem { Header = "Delete exam" };
            examContextMenu.Items.Add(ma11);
            examContextMenu.Items.Add(ma12);

            imageContextMenu = new ContextMenu();
            MenuItem ma22 = new MenuItem { Header = "delete image" };
            MenuItem ma13 = new MenuItem { Header = "Pokaz obrazek" };
            //ma13.Command = ShowImage;
            imageContextMenu.Items.Add(ma22);
            imageContextMenu.Items.Add(ma13);
           

            projectExplorerTreeViewItems = new List<TreeViewItemWithIcon>();
            WorkspaceMetadata workspaceData = voctsCore.Data.WorkspaceData;
            foreach (ProjectMetadata projectData in workspaceData.WorkspaceProjects)
                AddProjectToTree(projectData);
        }

        private void AddProjectToTree(ProjectMetadata projectData)
        {
            TreeViewItemWithIcon projectTreeItem = new TreeViewItemWithIcon();
            projectTreeItem.textBlock.Text =projectData.ProjectName;
            projectTreeItem.ItemId = projectData.ProjectID;
            projectTreeItem.Parent = null;
            foreach (ExaminationMetadata examinationData in projectData.ProjectExaminations)
                AddExaminationToTree(projectTreeItem, examinationData);
            projectTreeItem.ContextMenu = projectContextMenu;
            projectTreeItem.icon.Source = TreeViewItemWithIcon.ProjectIconSource;
            projectExplorerTreeViewItems.Add(projectTreeItem);
        }

        private void AddExaminationToTree(TreeViewItemWithIcon projectTreeItem, ExaminationMetadata examinationData)
        {
            TreeViewItemWithIcon examinationTreeItem = new TreeViewItemWithIcon();
            examinationTreeItem.textBlock.Text =examinationData.ExaminationName;
            examinationTreeItem.ItemId = examinationData.ExaminationID;
            examinationTreeItem.Parent = projectTreeItem;
            foreach (PotatoSoft.VOCTS.Workbench.ImageMetadata imageData in examinationData.ExaminationImages)
                AddImgaeToTree(examinationTreeItem, imageData);
            examinationTreeItem.ContextMenu = examContextMenu;
            examinationTreeItem.icon.Source = TreeViewItemWithIcon.ExaminationIconSource;
            projectTreeItem.Items.Add(examinationTreeItem);
        }

        private void AddImgaeToTree(TreeViewItemWithIcon examinationTreeItem, PotatoSoft.VOCTS.Workbench.ImageMetadata imageData)
        {
            TreeViewItemWithIcon imageTreeItem = new TreeViewItemWithIcon();
            imageTreeItem.ItemId = imageData.ImageID;
            imageTreeItem.Parent = examinationTreeItem;
            imageTreeItem.textBlock.Text = imageData.ImageName;
            (imageContextMenu.Items.GetItemAt(1) as MenuItem).CommandParameter = imageData.ImageID;
            imageTreeItem.ContextMenu = imageContextMenu;
            imageTreeItem.icon.Source = TreeViewItemWithIcon.ImageIconSource;
            examinationTreeItem.Items.Add(imageTreeItem);
        }

        #region Project Explorer Tree View Items

        public const string ProjectExplorerTreeViewItemsPropertyName = "ProjectExplorerTreeViewItems";
        private List<TreeViewItemWithIcon> projectExplorerTreeViewItems;
        public IEnumerable<TreeViewItemWithIcon> ProjectExplorerTreeViewItems
        {
            get { return projectExplorerTreeViewItems; }
        }
        #endregion

        private void ShowImage(TreeViewItemWithIcon imageTreeViewItem)
        {
            if (imageTreeViewItem.iconSource != TreeViewItemWithIcon.ImageIconSource)
                return;
            ImagePotato_8u imagePotato=(ImagePotato_8u)voctsCore.Data.GetImage(imageTreeViewItem.Parent.Parent.ItemId, imageTreeViewItem.Parent.ItemId, imageTreeViewItem.ItemId);
            ImageControlViewModel imageControlVM = new ImageControlViewModel(imagePotato);
            Messenger.Default.Send<ShowImageMessage>(new ShowImageMessage(imageControlVM));
        }
    }
}
